package tech.aistar.prj.controller;

import tech.aistar.prj.entity.Pet;
import tech.aistar.prj.entity.PetType;
import tech.aistar.prj.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/16 20:13
 */
@WebServlet(urlPatterns = "/pet/view")
public class PetViewAction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        //从request作用域中获取数据
        List<Pet> pets=(List<Pet>)req.getAttribute("pets");
        List<PetType> typeList= (List<PetType>) req.getAttribute("types");

        User user= (User) req.getSession().getAttribute("user");

        out.write("<html>\n");
        out.write("<head>\n");
        out.write("    <title></title>\n");
        out.write("    <meta charset=\"UTF-8\">\n");
        out.write("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
        out.write("    <link rel=\"stylesheet\" href=\"/lk/plugins/bootstrap-3.3.7-dist/css/bootstrap.css\">\n");
        out.write("    <link rel=\"stylesheet\" href=\"/lk/css/index.css\">\n");
        out.write("</head>\n");
        out.write("<body>\n");
        out.write("    <div class=\"container-fluid\">\n");
        out.write("        <div class=\"row\">\n");
        out.write("            <nav class=\"navbar navbar-default\" role=\"navigation\">\n");
        out.write("                <div class=\"container-fluid\">\n");
        out.write("                    <div class=\"navbar-header\">\n");
        out.write("                        <button type=\"button\" class=\"navbar-toggle\" data-toggle=\"collapse\"\n");
        out.write("                                data-target=\"#example-navbar-collapse\">\n");
        out.write("                            <span class=\"sr-only\">切换导航</span>\n");
        out.write("                            <span class=\"icon-bar\"></span>\n");
        out.write("                            <span class=\"icon-bar\"></span>\n");
        out.write("                            <span class=\"icon-bar\"></span>\n");
        out.write("                        </button>\n");
        out.write("                        <a class=\"navbar-brand\" href=\"#\">宠物网站</a>\n");
        out.write("                    </div>\n");
        out.write("                    <div class=\"collapse navbar-collapse\" id=\"example-navbar-collapse\">\n");
        out.write("                        <ul class=\"nav navbar-nav\">\n");
        if(null!=user){
            out.write("                            <li><a href='/lk/pet/indexData'>"+user.getUserName()+"</a></li>\n");
        }

        out.write("                            <li><a href='/lk/pet/indexData'>首页</a></li>\n");
        out.write("                            <li class=\"active\"><a href='/lk/user/loginView'>登录</a></li>\n");
        out.write("                            <li><a href=\"#\">注册</a></li>\n");
        //只有登录之后，才会显示
        //只要登录成功，user就放入到session中
        if(null!=user){
            out.write("                            <li><a href='/lk/user/exit'>安全退出</a></li>\n");
            out.write("                            <li><a href=\"#\">我的关注</a></li>\n");
        }
        out.write("                        </ul>\n");
        out.write("                    </div>\n");
        out.write("                </div>\n");
        out.write("            </nav>\n");
        out.write("        </div>\n");
        out.write("    </div>\n");
        out.write("\n");
        out.write("    <div class=\"container-fluid\">\n");
        out.write("        <div class=\"row\">\n");
        out.write("            <div class=\"col-md-2\">\n");
        out.write("                <form role=\"form\">\n");
        out.write("                    <div class=\"form-group\">\n");
        out.write("                        <select id='petTypeSelect' class=\"form-control\">\n");
        out.write("                            <option value=\"-1\">===宠物类型===</option>\n");

        //下拉框数据回显
        String typeId=(String)req.getAttribute("typeId");

        for (PetType t : typeList) {
            if(t.getId().equals(Long.valueOf(typeId))){
                out.write("                            <option selected value='"+t.getId()+"'>"+t.getName()+"</option>\n");
            }else {
                out.write("                            <option value='"+t.getId()+"'>"+t.getName()+"</option>\n");
            }
        }

        out.write("                        </select>\n");
        out.write("                    </div>\n");
        out.write("                </form>\n");
        out.write("            </div>\n");
        out.write("            <div class=\"col-md-8\">\n");
        out.write("                <div>\n");
        out.write("                    <form class=\"bs-example bs-example-form\" role=\"form\">\n");
        out.write("                        <div class=\"row\">\n");
        out.write("                            <div class=\"col-lg-6\">\n");
        out.write("                                <div class=\"input-group\">\n");
        out.write("                                    <input value='"+req.getAttribute("name")+"' id='search_input' type=\"text\" class=\"form-control\">\n");
        out.write("                                    <span class=\"input-group-btn\">\n");
        out.write("                        <button class=\"btn btn-default\" id='search_btn' type=\"button\">搜索</button>\n");
        out.write("                                        <button type='button' id='more_del_btn' style=\"margin-left: 5px\" class=\"btn btn-danger\">批量删除</button>\n");
        out.write("                <button class=\"btn btn-info\">增加宠物</button>\n");
        out.write("                    </span>\n");
        out.write("                                </div><!-- /input-group -->\n");
        out.write("                            </div><!-- /.col-lg-6 -->\n");
        out.write("                        </div><!-- /.row -->\n");
        out.write("                    </form>\n");
        out.write("                </div>\n");
        out.write("            </div>\n");
        out.write('\n');
        out.write('\n');
        out.write("\n");
        out.write("        </div>\n");
        out.write("    </div>\n");
        out.write("\n");
        out.write("    <div class=\"container-fluid\" style=\"margin-top: 50px\">\n");
        out.write("        <div class=\"table-responsive\">\n");
        out.write("            <table class=\"table\">\n");
        out.write("                <caption>宠物信息</caption>\n");
        out.write("                <thead>\n");
        out.write("                    <tr>\n");
        out.write("                        <th><input id='ck_all' type=\"checkbox\"></th>\n");
        out.write("                        <th>序号</th>\n");
        out.write("                        <th>宠物头像</th>\n");
        out.write("                        <th>宠物名称</th>\n");
        out.write("                        <th>宠物年龄</th>\n");
        out.write("                        <th>出生日期</th>\n");
        out.write("                        <th>操作选项</th>\n");
        out.write("                    </tr>\n");
        out.write("                    </thead>\n");
        out.write("                <tbody id='tb_pet' class='pet'>\n");

        for(int i=0;i<pets.size();i++) {
            out.write("                    <tr>\n");
            out.write("                        <td><input value='"+pets.get(i).getId()+"' name='ck_content' type=\"checkbox\"></td>\n");
            out.write("                        <td>"+(i+1)+"</td>\n");
            out.write("                        <td><img src='/lk/img/"+pets.get(i).getImgUrl()+"'></td>\n");
            out.write("                        <td>"+pets.get(i).getName()+"</td>\n");
            out.write("                        <td>"+pets.get(i).getAge()+"</td>\n");
            out.write("                        <td>"+pets.get(i).getBirthday()+"</td>\n");
            out.write("                        <td>待发货</td>\n");
            out.write("                        <td><a class=\"text-danger\" href='/lk/pet/del_one?id="+pets.get(i).getId()+"'><span class=\"glyphicon glyphicon-trash\"></span></a>\n");
            out.write("                            <button class='btn btn-link' onclick=ajax_del(this,'"+pets.get(i).getId()+"')>异步删除</button>\n");
            out.write("                            <a class=\"text-warning\" href=\"#\"><span class=\"glyphicon glyphicon-edit\"></span></a></td>\n");
            out.write("                    </tr>\n");
        }
        out.write("                </tbody>\n");
        out.write("            </table>\n");
        out.write("        </div>\n");
        out.write("    </div>\n");
        out.write("\n");
        out.write("\n");
        out.write("<script src=\"/lk/plugins/jquery/jquery.min.js\"></script>\n");
        out.write("<script src=\"/lk/plugins/bootstrap-3.3.7-dist/js/bootstrap.js\"></script>\n");
        out.write("<script src=\"/lk/js/index.js\"></script>\n");
        out.write("</body>\n");
        out.write("</html>\n");
    }
}
